/*
 * Copyright (C) 2006 Roman Krashanitsa
 *
 * @author Roman Krashanitsa
 *
 * This file is part of paparazzi.
 *
 * paparazzi is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * paparazzi is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with paparazzi; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA. 
 *
 */


package pprz.components;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class Slider extends Component 
implements MouseListener, MouseMotionListener {

	static final long serialVersionUID=1L;
	private double pos;
	private boolean showing;
	private int x0=2,y0=2;
	private double dpos=0.1;

	public Slider(double pos0)
	{
		super();
		pos=pos0>0?(pos0<1?pos0:1):0;
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
//		this.setVisible(true);
		showing=false;
	}
	
	public double getPos()
	{return pos;}
	@Override
	public void paint(Graphics g) {
		if (!showing) return;
		int h=this.getHeight()-y0;
		int w=this.getWidth()-x0;
		int d=h<w?h/5:w/5;
		super.paint(g);
		Graphics2D g2=(Graphics2D)g;
		g2.setRenderingHint (RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setStroke(new BasicStroke(1));
		//g2.setColor(new Color(0.f,0.f,0.f,0.5f));
		g2.setColor(Color.darkGray);
		g2.drawRoundRect(x0+(5*w-h)/10, y0, h/5, h/5, d, d);
		g2.drawLine(x0+(w-w/10)/2, y0+h/5, x0+(w-w/10)/2, h-h/5);
		g2.drawLine(x0+w/2+w/20, y0+h/5, x0+w/2+w/20, h-h/5);
		g2.drawRoundRect(x0+(5*w-h)/10, y0+4*h/5, h/5, h/5, d, d);
		g2.setColor(new Color(1f,1f,1f,0.5f));
		g2.fillRoundRect(x0+(5*w-h)/10,(int)(y0+h/5+((3*h/5.0-h/10)*(1-pos))), h/5, h/10, d, d);
		g2.fillRect(x0+(5*w-h)/10+(h/5-h/8)/2, y0+(h/5-h/20)/2, h/8, h/20);
		g2.fillRect(x0+(5*w-h)/10+(h/5-h/20)/2, y0+(h/5-h/8)/2, h/20, h/8);
		g2.fillRect(x0+(5*w-h)/10+(h/5-h/8)/2, y0+4*h/5+(h/5-h/20)/2, h/8, h/20);
		g2.setColor(Color.darkGray);
		g2.drawRoundRect(x0+(5*w-h)/10,(int)(y0+h/5+((3*h/5.0-h/10)*(1-pos))), h/5, h/10, d, d);
	}



	@Override
	public void update(Graphics arg0) {
		super.update(arg0);
	}

	public void mouseClicked(MouseEvent arg0) {
		
	}

	public void mouseEntered(MouseEvent arg0) {
		showing=true;
		repaint();
	}

	public void mouseExited(MouseEvent arg0) {
		showing=false;
		repaint();
	}

	public void mousePressed(MouseEvent arg0) {
		if (arg0.getButton()!=MouseEvent.BUTTON1) return;
		int h=this.getHeight();
		int w=this.getWidth();
		int x=arg0.getX();
		int y=arg0.getY();
		if (y-y0<h/5 && Math.abs(x-x0-w/2)<h/5)
		{
			// + is pressed
			//System.out.println("+");
			this.firePropertyChange("pos", pos, pos+dpos);
			pos+=dpos; if (pos>1) pos=1;
		}
		else if ((y-y0>h-h/5 && Math.abs(x-x0-w/2)<h/5))
		{
			// + is pressed
			//System.out.println("-");
			this.firePropertyChange("pos", pos, pos-dpos);
			pos-=dpos; if (pos<0) pos=0;
		}
		else if (Math.abs(x-x0-w/2)<h/5)
		{
			// slider pressed
			System.out.print("slider-");
			if (y-y0>h/5+((3*h/5.0-y0-h/10)*(1-pos)) && 
				y-y0<h/5+((3*h/5.0-y0+h/10)*(1-pos)))
			{
				//System.out.println("mover");
			}
			else
			{
				//System.out.println("grade");
			}
				
		}
		repaint();
		
	}

	public void mouseReleased(MouseEvent arg0) {
		
	}

	public void mouseDragged(MouseEvent e) {
		
	}

	public void mouseMoved(MouseEvent e) {
		
	}
	
	
	
	
}
